<?php
$company = '<a href="https://code.google.com/p/auto-listing/">Auto-Listing</a>, @<a href="http://www.sanjaal.com/">Sanjal Corps</a>';

# Silent please...
error_reporting(0);
ini_set('display_errors', 'Off');

# Force rechecking URLs in every 4 hours
$seconds = 4 * 60 * 60;

# Affects <base href="?" />
# Where to search for files? Generally point to your public_html location.
# We assume that "../auto-listing/" is the root of your public_html.
$lookup_dir = (dirname(dirname(__FILE__)));

# Full URL to home page root
$homepage = 'http://'.$_SERVER['SERVER_NAME'].'/';

# Connect to the database.
$is_mysql=false;
$connection = mysql_connect('localhost', 'root', 'toor', false) or $is_mysql = false; # die('Cannot connect to the database.');
if($is_mysql) mysql_select_db('smarty_framework', $connection) or $is_mysql = false; # die('Cannot use the database.');
define('IS_MYSQL', $is_mysql);

define('__MYSELF__', dirname(__FILE__));
define('TIMEOUT', 2); # Responses should be too fast

# Load the dependencies
require_once('classes/class.response.inc.php');  # HTTP Response Reader
require_once('classes/class.info.inc.php');      # Reader/Parser for info.xml file
require_once('classes/class.linker.inc.php');    # Makes links around directory/file names

/**
* Checks whether a URL exists.
* If so, it updates the time when it last checcked for the validitiy.
*/
function url_exists($domain_url='', $domain_id=0)
{
	if(!IS_MYSQL) return false;
	
	$response = new response();
	$url_exists = $response->url_exists($domain_url);
	$is_valid = ($url_exists)?'Y':'N';
	
	# Update the domain response codes "everytime"?
	$domain_id = (int)$domain_id;
	$response_code = (int)$response->http_code;

	$update_sql="
UPDATE localhost_domains SET
	# Prevents too frequent re-checking
	checked_on = UNIX_TIMESTAMP(CURRENT_TIMESTAMP()),
	check_counter = check_counter+1,

	# Currently found response code
	response_code={$response_code},

	# Did the URL exist?
	is_valid='{$is_valid}',
	
	# Once validated, do not attempt it in next time.
	# User will automatically request for validations if required.
	# Manually reset this flag to Y.
	do_validate='N'
WHERE
	domain_id={$domain_id}
;";
	mysql_query($update_sql);
	return $url_exists;
}

/**
* Cuts OFF a URL if not valid
*/
function recheck_url($domain_url='', $domain_id=0, $domain_name='')
{
	if(!url_exists($domain_url, $domain_id))
	{
		# This URL is not available to browse. Cross-Mark it.
		$domain_name = "<del><span>{$domain_name}</span></del>";
	}
	return $domain_name;
}
?>